package com.studyweb.service.impl;

import com.studyweb.mapper.WrongQuestionMapper;
import com.studyweb.pojo.entity.WrongQuestion;
import com.studyweb.service.WrongQuestionService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 错题本服务实现类
 */
@Service
public class WrongQuestionServiceImpl implements WrongQuestionService {
    
    @Autowired
    private WrongQuestionMapper wrongQuestionMapper;
    
    @Override
    public Map<String, Object> getQuestions(int pageNum, int pageSize, String questionType, String difficulty, String category, String module, String keyword) {
        // 使用PageHelper进行分页
        PageHelper.startPage(pageNum, pageSize);
        
        // 使用selectQuestions方法进行分页查询，只需要传递查询条件参数
        List<WrongQuestion> questions = wrongQuestionMapper.selectQuestions(
            0, 0, questionType, difficulty, category, module, keyword);
        int total = wrongQuestionMapper.countQuestions(questionType, difficulty, category, module, keyword);
        
        Map<String, Object> result = new HashMap<>();
        result.put("questions", questions);
        result.put("total", total);
        result.put("page", pageNum);
        result.put("pageSize", pageSize);
        result.put("totalPages", (int) Math.ceil((double) total / pageSize));
        
        return result;
    }
    
    @Override
    @Transactional
    public boolean createQuestion(WrongQuestion question) {
        question.setCreateTime(LocalDateTime.now());
        question.setUpdateTime(LocalDateTime.now());
        question.setStatus(1);
        
        return wrongQuestionMapper.insert(question) > 0;
    }
    
    @Override
    public WrongQuestion getQuestionById(Long id) {
        return wrongQuestionMapper.selectById(id);
    }
    
    @Override
    @Transactional
    public boolean updateQuestion(WrongQuestion question) {
        question.setUpdateTime(LocalDateTime.now());
        return wrongQuestionMapper.updateById(question) > 0;
    }
    
    @Override
    @Transactional
    public boolean deleteQuestion(Long id) {
        return wrongQuestionMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional
    public boolean batchDeleteQuestions(List<Long> ids) {
        return wrongQuestionMapper.batchDeleteByIds(ids) > 0;
    }
    
    @Override
    public List<WrongQuestion> getPracticeQuestions(Integer count, String difficulty, String category, String module) {
        // 默认按照question_id升序排列
        return getPracticeQuestions(count, difficulty, category, module, "question_id", "asc");
    }
    
    // 添加一个新的重载方法，支持自定义排序
    public List<WrongQuestion> getPracticeQuestions(Integer count, String difficulty, String category, String module, String orderBy, String orderType) {
        // 使用PageHelper处理分页
        if (count != null && count > 0) {
            PageHelper.startPage(1, count);
        }
        return wrongQuestionMapper.selectPracticeQuestions(count, difficulty, category, module, orderBy, orderType);
    }
    
    @Override
    public Map<String, Object> submitAnswer(Map<String, Object> answerData) {
        // 实现提交答案的逻辑
        return new HashMap<>();
    }
    
    @Override
    public Map<String, Object> getResults(int pageNum, int pageSize) {
        // 实现获取答题结果的逻辑
        return new HashMap<>();
    }
}